<?php
declare (strict_types=1);

namespace app\controller;

use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Kernel\Factory;
use app\BaseController;
use app\common\GptService;
use app\common\QuestionService;
use app\common\TencentIm;
use app\common\UsersService;
use app\extend\alipayglobal\DefaultAlipayClient;
use app\extend\alipayglobal\model\Amount;
use app\extend\alipayglobal\model\Order;
use app\extend\alipayglobal\model\ProductCodeType;
use app\extend\alipayglobal\request\order\AlipayCreateOrderRequest;
use app\job\Task;
use app\model\Activity;
use app\model\ActivityComment;
use app\model\Forum;
use app\model\ForumComment;
use app\model\Friends;
use app\model\FriendsComment;
use app\model\IndexClass;
use app\model\News;
use app\model\NewsComment;
use app\model\Question;
use app\model\QuestionAnswer;
use app\model\QuestionAnswerComment;
use app\model\Recruit;
use app\model\RecruitComment;
use app\model\Room;
use app\model\RoomComment;
use app\model\SecGoods;
use app\model\SecGoodsComment;
use app\model\Store;
use app\model\StoreAssess;
use app\model\StoreAssessComment;
use app\model\Talent;
use app\model\TalentComment;
use app\model\TalentDocument;
use app\model\TalentGoodsOrders;
use app\model\Test;
use app\model\Users;
use app\model\UsersRelated;
use app\Request;
use app\tool\Http;
use think\db\exception\DbException;
use think\db\Query;
use think\Exception;
use think\facade\Queue;
use think\response\Json;


class IndexController extends BaseController
{
	protected array $middleware = [
		'Login' => ['except' => ['index', 'class', 'test', 'search', 'hot_talent', 'tests', 'start']],
		'Location' => ['only' => ['index']],
	];
	
	public $postCharset = "UTF-8";
	private $fileCharset = "UTF-8";
	
	
	function test(Request $request)
	{
        $request->users_id = 833;
        $rows = UsersRelated::with(['from_users'])->filter(function ($data) use ($request) {

            switch ($data->type) {
                case 2:
                case 4:
                case 1:
                case 3:
                case 7:
                case 5:
                case 8:
                case 9:

                    switch ($data->scene) {
                        case 'Activity':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = ActivityComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'activity_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->activity_id;
                                if ($data->type == 1) {
                                    $p_comment = ActivityComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = Activity::find($data->pk_id);
                            if ($data->type == 2) {
                                $msg = '评论了你的优惠资讯';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的优惠资讯';
                            }
                            if ($data->type == 7) {
                                $msg = '收藏了你的优惠资讯';
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你优惠资讯的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你优惠资讯的评论';
                            }
                            if ($data->type == 8) {
                                $msg = '在优惠资讯的评论中@了你';
                            }
                            break;
                        case 'Forum':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = ForumComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'forum_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->forum_id;
                                if ($data->type == 1) {
                                    $p_comment = ForumComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = Forum::withTrashed()->find($data->pk_id);
                            if ($data->type == 2) {
                                $msg = '评论了你的论坛';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的论坛';
                            }
                            if ($data->type == 7) {
                                $msg = '收藏了你的论坛';
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你论坛的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你论坛的评论';
                            }
                            if ($data->type == 8) {
                                $msg = '在论坛的评论中@了你';
                            }
                            break;
                        case 'Friends':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = FriendsComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'friends_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->friends_id;
                                if ($data->type == 1) {
                                    $p_comment = FriendsComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = Friends::withTrashed()->find($data->pk_id);
                            $data->friends_type = $pro->type;
                            if ($pro->type == 1) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的交友';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的交友';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的交友';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你交友的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你交友的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在交友的评论中@了你';
                                }
                            }
                            if ($pro->type == 2) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的活动';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的活动';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的活动';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你活动的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你活动的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在活动的评论中@了你';
                                }
                            }
                            break;
                        case 'News':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = NewsComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'news_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->news_id;
                                if ($data->type == 1) {
                                    $p_comment = NewsComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = News::find($data->pk_id);
                            if ($data->type == 2) {
                                $msg = '评论了你的新闻资讯';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的新闻资讯';
                            }
                            if ($data->type == 7) {
                                $msg = '收藏了你的新闻资讯';
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你新闻资讯的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你新闻资讯的评论';
                            }
                            if ($data->type == 8) {
                                $msg = '在新闻资讯的评论中@了你';
                            }
                            break;
                        case 'QuestionAnswer':
                            if (!in_array($data->type, [3, 7, 5, 9])) {
                                $comment = QuestionAnswerComment::withTrashed()->with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'question_answer_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->answer_id;
                                if ($data->type == 1) {
                                    $p_comment = QuestionAnswerComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = QuestionAnswer::find($data->pk_id);
                            if ($data->type == 5) {
                                $data->answer = $pro;
                            }
                            if ($data->type == 2) {
                                $msg = '评论了你的回答';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的回答';
                            }
                            if ($data->scene == 'QuestionAnswer') {
                                if ($data->type == 7) {
                                    $msg = '收藏了你的回答';
                                }
                            } else {
                                if ($data->type == 7) {
                                    $msg = '收藏了你的问题';
                                }
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你回答的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你回答的评论';
                            }
                            if ($data->type == 9) {
                                $msg = '题主采纳了你的回答';
                            }
                            if ($data->type == 5) {
                                $msg = '回复了你的问题';
                            }
                            if ($data->type == 8) {
                                $msg = '在回答的评论中@了你';
                            }
                            break;
                        case 'Question':
                            if (!in_array($data->type, [3, 7, 5, 9])) {
                                $comment = QuestionAnswerComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'question_answer_comment']);
                                }])->find($data->pro_id);

                                $data->comment = $comment;
                                $data->pk_id = $comment->answer_id;
                                $question_id = $comment->question_id;
                                if ($data->type == 1) {
                                    $p_comment = QuestionAnswerComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                                $question_id = $data->pro_id;
                            }
                            $pro = Question::withTrashed()->find($question_id);
                            if ($data->type == 5) {
                                $pro = QuestionAnswer::find($data->pk_id);
                                $data->answer = $pro;
                            }
                            if ($data->type == 2) {
                                $msg = '评论了你的回答';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的回答';
                            }
                            if ($data->scene == 'QuestionAnswer') {
                                if ($data->type == 7) {
                                    $msg = '收藏了你的回答';
                                }
                            } else {
                                if ($data->type == 7) {
                                    $msg = '收藏了你的问题';
                                }
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你回答的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你回答的评论';
                            }
                            if ($data->type == 9) {
                                $msg = '题主采纳了你的回答';
                            }
                            if ($data->type == 5) {
                                $msg = '回复了你的问题';
                            }
                            if ($data->type == 8) {
                                $msg = '在回答的评论中@了你';
                            }
                            break;
                        case 'Recruit':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = RecruitComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'recruit_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->recruit_id;
                                if ($data->type == 1) {
                                    $p_comment = RecruitComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = Recruit::find($data->pk_id);
                            if ($pro->type == 0) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的招聘';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的招聘';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的招聘';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你招聘的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你招聘的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在招聘的评论中@了你';
                                }
                            }
                            if ($pro->type == 1) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的求职';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的求职';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的求职';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你求职的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你求职的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在求职的评论中@了你';
                                }
                            }
                            break;
                        case 'Room':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = RoomComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'room_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->room_id;
                                if ($data->type == 1) {
                                    $p_comment = RoomComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = Room::withTrashed()->find($data->pk_id);
                            if ($pro->type == 1) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的出租';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的出租';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏你的出租';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你出租的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你出租的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在出租的评论中@了你';
                                }
                            }
                            if ($pro->type == 2) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的求租';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的求租';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的求租';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你求租的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你求租的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在求租的评论中@了你';
                                }
                            }
                            if ($pro->type == 3) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的出售';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的出售';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的出售';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你出售的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你出售的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在出售的评论中@了你';
                                }
                            }

                            break;
                        case 'SecGoods':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = SecGoodsComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'secgoods_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->goods_id;
                                if ($data->type == 1) {
                                    $p_comment = SecGoodsComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = SecGoods::withTrashed()->find($data->pk_id);
                            if ($data->type == 2) {
                                $msg = '评论了你的二手物品';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的二手物品';
                            }
                            if ($data->type == 7) {
                                $msg = '收藏了你的二手物品';
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你二手物品的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你二手物品的评论';
                            }
                            if ($data->type == 8) {
                                $msg = '在二手物品的评论中@了你';
                            }
                            break;
                        case 'Store':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = StoreAssessComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'store_assess_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->assess_id;
                                if ($data->type == 1) {
                                    $p_comment = StoreAssessComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = StoreAssess::withTrashed()->find($data->pk_id);
                            if ($data->type == 2) {
                                $msg = '评论了你的商家评价';
                            }
                            if ($data->type == 3) {
                                $msg = '赞了你的商家评价';
                            }
                            if ($data->type == 7) {
                                $msg = '收藏了你的商家评价';
                            }
                            if ($data->type == 4) {
                                $msg = '赞了你商家评价的评论';
                            }
                            if ($data->type == 1) {
                                $msg = '回复了你商家评价的评论';
                            }
                            if ($data->type == 8) {
                                $msg = '在商家评价的评论中@了你';
                            }
                            break;
                        case 'Talent':
                            if (!in_array($data->type, [3, 7])) {
                                $comment = TalentComment::with(['users_like' => function (Query $query) use ($request) {
                                    $query->where(['users_id' => $request->users_id, 'type' => 'talent_comment']);
                                }])->find($data->pro_id);
                                $data->comment = $comment;
                                $data->pk_id = $comment->talent_id;
                                if ($data->type == 1) {
                                    $p_comment = TalentComment::find($comment->pid);
                                    $data->p_comment = $p_comment;
                                }
                            } else {
                                $data->pk_id = $data->pro_id;
                            }
                            $pro = Talent::withTrashed()->find($data->pk_id);
                            if ($pro->type == 1) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的优才笔记';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的优才笔记';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的优才笔记';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你优才笔记的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你优才笔记的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在优才笔记的评论中@了你';
                                }
                            }
                            if ($pro->type == 2) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的高才笔记';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的高才笔记';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的高才笔记';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你高才笔记的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你高才笔记的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在高才笔记的评论中@了你';
                                }
                            }
                            if ($pro->type == 3) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的专才笔记';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的专才笔记';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的专才笔记';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你专才笔记的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你专才笔记的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在专才笔记的评论中@了你';
                                }
                            }
                            if ($pro->type == 4) {
                                if ($data->type == 2) {
                                    $msg = '评论了你的留学笔记';
                                }
                                if ($data->type == 3) {
                                    $msg = '赞了你的留学笔记';
                                }
                                if ($data->type == 7) {
                                    $msg = '收藏了你的留学笔记';
                                }
                                if ($data->type == 4) {
                                    $msg = '赞了你留学笔记的评论';
                                }
                                if ($data->type == 1) {
                                    $msg = '回复了你留学笔记的评论';
                                }
                                if ($data->type == 8) {
                                    $msg = '在留学笔记的评论中@了你';
                                }
                            }
                            break;
                        default:
                            writeJson(0, '列表展示错误');
                    }
                    if ($pro->images){
                        $image = explode(',', $pro->images) ? explode(',', $pro->images)[0] : '';
                    }else{
                        $image = '';
                    }

                    $data->image = $image;
                    $data->author = 0;
                    $bot = Users::getFieldById($data->from_users_id,'bot');
                    if ($data->scene!='QuestionAnswer'&&$data->from_users_id == $pro->users_id && $bot!= 1) {
                        $data->author = 1;
                    }
                    break;
                case 6:
                    $pro = TalentDocument::find($data->pro_id);
                    $data->document = $pro;
                    $msg = '叮！"AI智能"已经为您生成结果，快去看看吧';
                    break;
                default:
                    writeJson(0, '列表展示错误');
            }
            $data->msg = $msg;
        })
            ->where(['users_id' => $request->users_id])
            ->order(['isread' => 'asc', 'id' => 'desc'])
            ->paginate()
            ->items();
        writeJson($rows);
	}
	
	/**
	 * 首页
	 * @return Json
	 */
	public function index(Request $request)
	{
		$lat = self::$latitude;
		$lng = self::$longitude;
		$pi = pi();
		$radius = 6371000;
		$store_list = Store::field("*,ACOS(SIN(($lat* $pi) / 180) * SIN((latitude * $pi) / 180 ) + COS(($lat* $pi) / 180 ) * COS((latitude * $pi) / 180 ) *COS(($lng* $pi) / 180 - (longitude * $pi) / 180 ) ) * $radius as distance")
			->order(['hot' => 'desc'])
			->where('status', 1)
			->limit(15)
			->select();
		$news_list = News::with(['issuer'])
			->order(['browse' => 'desc'])
			->limit(5)
			->select();
		$forum_list = Forum::with([
				'users',
				'users_like' => function (Query $query) use ($request) {
					$query->where(['users_id' => $request->users_id, 'type' => 'forum']);
				}]
		)
			->order('browse', 'desc')
			->limit(5)
			->select();
//        $talent_list = Talent::with(['users'])
////            ->where('is_good',1)
//            ->order('id', 'desc')
//            ->limit(5)
//            ->select();
		$arr['hot_store'] = $store_list;
		$arr['hot_news'] = $news_list;
		$arr['hot_forum'] = $forum_list;
//        $arr['hot_talent'] = $talent_list;
		writeJson($arr);
	}
	
	function class()
	{
		$rows = IndexClass::where(['status' => 1])->order(['weigh' => 'desc'])->select();
		writeJson($rows);
	}
	
	/**
	 * 热门人才
	 * @return void
	 */
	function hot_talent()
	{
		$rows = Talent::with(['users'])
//            ->where('is_good',1)
			->orderRaw('rand()')
			->limit(100)
			->select();
		writeJson($rows);
	}
	
	/**
	 * 搜索
	 * @param string $keyword
	 * @return Json
	 * @throws DbException
	 */
	function search(string $keyword)
	{
		if (empty($keyword)) {
			writeJson(0, '无效关键词');
		}
		$list['Question']['count'] = Question::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['Room']['count'] = Room::whereLike('room_name', '%' . $keyword . '%')->count();
		$list['Room']['types'] = [
			['type' => 1, 'name' => '出租', 'count' => Room::where(['type' => 1])->whereLike('room_name', '%' . $keyword . '%')->count()],
			['type' => 2, 'name' => '求租', 'count' => Room::where(['type' => 2])->whereLike('room_name', '%' . $keyword . '%')->count()],
			['type' => 3, 'name' => '出售', 'count' => Room::where(['type' => 3])->whereLike('room_name', '%' . $keyword . '%')->count()],
		];
		$list['News']['count'] = News::whereLike('title_s|content_s', '%' . $keyword . '%')->whereTime('createtime', '-7 days')->count();
		$list['SecGoods']['count'] = SecGoods::whereLike('content', '%' . $keyword . '%')->count();
		$list['Store']['count'] = Store::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['StoreAssess']['count'] = StoreAssess::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['Forum']['count'] = Forum::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['Activity']['count'] = Activity::whereLike('title', '%' . $keyword . '%')->count();
		$list['Activity']['types'] = [
			['type' => 1, 'name' => '线上活动', 'count' => Activity::where(['type' => 1])->whereLike('title', '%' . $keyword . '%')->count()],
			['type' => 2, 'name' => '线下活动', 'count' => Activity::where(['type' => 2])->whereLike('title', '%' . $keyword . '%')->count()],
		];
		$list['Recruit']['count'] = Recruit::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['Recruit']['types'] = [
			['type' => 0, 'name' => '招聘', 'count' => Recruit::where(['type' => 0])->whereLike('title|content', '%' . $keyword . '%')->count()],
			['type' => 1, 'name' => '求职', 'count' => Recruit::where(['type' => 1])->whereLike('title|content', '%' . $keyword . '%')->count()],
		];
		$list['Talent']['count'] = Talent::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['Talent']['types'] = [
			['type' => 1, 'name' => '优才计划', 'count' => Talent::where(['type' => 1])->whereLike('title|content', '%' . $keyword . '%')->count()],
			['type' => 2, 'name' => '高才计划', 'count' => Talent::where(['type' => 2])->whereLike('title|content', '%' . $keyword . '%')->count()],
			['type' => 3, 'name' => '专才计划', 'count' => Talent::where(['type' => 3])->whereLike('title|content', '%' . $keyword . '%')->count()],
			['type' => 4, 'name' => '留学', 'count' => Talent::where(['type' => 4])->whereLike('title|content', '%' . $keyword . '%')->count()],
		];
		$list['Friends']['count'] = Friends::whereLike('title|content', '%' . $keyword . '%')->count();
		$list['Friends']['types'] = [
			['type' => 1, 'name' => '交友', 'count' => Friends::where(['type' => 1])->whereLike('title|content', '%' . $keyword . '%')->count()],
			['type' => 2, 'name' => '活动', 'count' => Friends::where(['type' => 2])->whereLike('title|content', '%' . $keyword . '%')->count()],
		];
		writeJson($list);
	}
	
	/**
	 * ip判断
	 * @param Request $request
	 * @return void
	 */
	function start(Request $request)
	{
		$ret = Http::get('https://restapi.amap.com/v3/ip?parameters', [
			'key' => 'b5cd86ae7b8a0819efa9490b08e16942',
			'ip' => $request->ip(),
		]);
		$ret = json_decode($ret);
		if (empty($ret->province)) {
			$ret = 0; //海外
		} else {
			$ret = 1; //国内
		}
		writeJson(1, '', $ret);
	}
	
	/**
	 * 校验$value是否非空
	 *  if not set ,return true;
	 *    if is null , return true;
	 **/
	protected function checkEmpty($value)
	{
		if (!isset($value))
			return true;
		if ($value === null)
			return true;
		
		
		return false;
	}
	
	/**
	 * 转换字符集编码
	 * @param $data
	 * @param $targetCharset
	 * @return string
	 */
	function characet($data, $targetCharset)
	{
		
		if (!empty($data)) {
			$fileType = $this->fileCharset;
			if (strcasecmp($fileType, $targetCharset) != 0) {
				$data = mb_convert_encoding($data, $targetCharset, $fileType);
			}
		}
		return $data;
	}
}
